package m.system.handler;

import com.alibaba.fastjson.JSONObject;
import m.system.exception.MException;
import m.system.util.ClassUtil;
import manage.model.AdminLogin;

import java.util.*;

public class HandlerUtil {
    private static final Map<String,Class<?>> classMap=new LinkedHashMap<>();
    private static final Map<String,HandlerMethodMeta> metaMap=new LinkedHashMap<>();
    private static final Map<String,String> methodMap=new LinkedHashMap<>();
    private static final Map<String,Integer> levelMap=new LinkedHashMap<>();

    protected static void addHandler(HandlerMethodMeta meta,Class<?> clazz,String method){
        classMap.put(meta.name(),clazz);
        metaMap.put(meta.name(),meta);
        methodMap.put(meta.name(),method);
        levelMap.put(meta.name(),meta.level());
        // 将LinkedHashMap转换为List以便排序
        List<Map.Entry<String, Integer>> list = new ArrayList<>(levelMap.entrySet());
        // 根据value值对List进行排序
        list.sort(Map.Entry.comparingByValue());
        // 创建一个新的LinkedHashMap并按照排序后的顺序插入元素
        levelMap.clear();
        for (Map.Entry<String, Integer> entry : list) {
            levelMap.put(entry.getKey(), entry.getValue());
        }
    }

    /**
     * 获取类相关的处理方法
     */
    public static Map<String,String> getAllHandler(String paramClass){
        List<String> ns=new ArrayList<>();
        for(String key : metaMap.keySet()){
            String className=metaMap.get(key).type().getName();
            if(className.equals(paramClass)
                ||className.equals(JSONObject.class.getName())){//JSONObject 为公共处理方法
                ns.add(key);
            }
        }
        Map<String,String> options=new LinkedHashMap<>();
        for(String key : levelMap.keySet()){
            if(ns.contains(key)){
                options.put(key,key+" "+metaMap.get(key).description());
            }
        }
        return options;
    }

    private static final Map<Class<?>,Object> serveMap=new HashMap<>();
    private static Object getServe(Class<?> serveClass) throws MException {
        Object serve=serveMap.get(serveClass);
        if(null==serve){
            serve=ClassUtil.newInstance(serveClass.getName());
            serveMap.put(serveClass,serve);
        }
        return serve;
    }
    /**
     * 调用数据处理方法
     */
    public static JSONObject executes(JSONObject model, AdminLogin admin,String... names) throws Exception {
        for(String name : names){
            model=execute(model,admin,name);
        }
        return model;
    }
    /**
     * 调用数据处理方法
     */
    public static JSONObject execute(JSONObject model, AdminLogin admin, String name) throws Exception {
        HandlerMethodMeta meta=metaMap.get(name);
        Class<?> pt=meta.type();
        if(null==pt) throw new MException(HandlerUtil.class,name+"数据处理不存在！");
        Object obj;
        boolean isJson=pt.getName().equals(JSONObject.class.getName());
        if(isJson) obj=model;
        else obj=JSONObject.toJavaObject(model,pt);
        Class<?> serveClass=classMap.get(name);
        String method=methodMap.get(name);
        Object serve=getServe(serveClass);
        if(meta.adminLogin()) obj=ClassUtil.executeMethod(serve,method,new Class<?>[]{pt,AdminLogin.class},new Object[]{obj,admin});
        else obj=ClassUtil.executeMethod(serve,method,new Class<?>[]{pt},new Object[]{obj});
        JSONObject result;
        if(isJson) result=(JSONObject) obj;
        else result= (JSONObject) JSONObject.toJSON(obj);
        return result;
    }
}
